﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;

using NUnitResults;

namespace NUnitResultsTests.Unit
{
    /// <summary>
    /// Tests the TestSuite class.
    /// </summary>
    public class TestSuiteTests
    {
        #region Constructor

        /// <summary>
        /// Tests the constructor of TestSuite.
        /// </summary>
        [Test]
        [TestCaseSource("GetConstructorTestCases")]
        public void TestConstructor(String name, String nspace, Status status, SuiteType suiteType, Nullable<TimeSpan> time, Nullable<Int32> asserts, IList<ITestResult> innerCases)
        {
            var t = new TestSuite(name, nspace, status, suiteType, time, asserts, innerCases);
            Assert.AreEqual(name, t.Name);
            Assert.AreEqual(nspace, t.Namespace);
            Assert.AreEqual(status, t.Status);
            Assert.AreEqual(suiteType, t.SuiteType);
            Assert.AreEqual(innerCases, t.InnerCases);
        }

        /// <summary>
        /// Provides test cases for the constructor test method.
        /// </summary>
        private IEnumerable<TestCaseData> GetConstructorTestCases()
        {
            var goodName = "Name";
            var goodNamespace = "Namespace";
            var goodStatus = Status.Success;
            var goodSuiteType = SuiteType.TestFixture;
            var goodTime = TimeSpan.FromSeconds(1);
            var goodAsserts = 1;
            var goodInnerCases = new List<ITestResult>();

            // Test name parameter.
            yield return new TestCaseData(null, goodNamespace, goodStatus, goodSuiteType, goodTime, goodAsserts, goodInnerCases).Throws(typeof(ArgumentNullException));
            yield return new TestCaseData(String.Empty, goodNamespace, goodStatus, goodSuiteType, goodTime, goodAsserts, goodInnerCases);
            yield return new TestCaseData("Name", goodNamespace, goodStatus, goodSuiteType, goodTime, goodAsserts, goodInnerCases);
            
            // Test nspace parameter.
            yield return new TestCaseData(goodName, null, goodStatus, goodSuiteType, goodTime, goodAsserts, goodInnerCases).Throws(typeof(ArgumentNullException));
            yield return new TestCaseData(goodName, String.Empty, goodStatus, goodSuiteType, goodTime, goodAsserts, goodInnerCases);
            yield return new TestCaseData(goodName, "Namespace", goodStatus, goodSuiteType, goodTime, goodAsserts, goodInnerCases);

            // Test innerCases parameter.
            yield return new TestCaseData(goodName, goodNamespace, goodStatus, goodSuiteType, goodTime, goodAsserts, null).Throws(typeof(ArgumentNullException));
            yield return new TestCaseData(goodName, goodNamespace, goodStatus, goodSuiteType, goodTime, goodAsserts, new List<ITestResult>());
            yield return new TestCaseData(goodName, goodNamespace, goodStatus, goodSuiteType, goodTime, goodAsserts, new List<ITestResult> { new TestCase("Case", "Namespace", Status.Success, TimeSpan.FromSeconds(1), 1) });
        }

        #endregion

        #region TestCaseCount

        /// <summary>
        /// Tests the TestCaseCount property.
        /// </summary>
        [Test]
        [TestCaseSource(typeof(NUnitResultsTests.Resources.TestCases), "GetSampleResults")]
        public void TestCount(String resourceFile, Int32 testCaseCount, Status status, IList<ITestResult> results)
        {
            Assert.AreEqual(testCaseCount, results.Single().TestCaseCount.Total);
        }

        #endregion

        #region FullName

        /// <summary>
        /// Test the FullName property.
        /// </summary>
        [Test]
        [TestCase("", "TestSuiteName", Result = "TestSuiteName")]
        [TestCase("Name.Space", "", Result = "Name.Space")]
        [TestCase("Name.Space", "TestSuiteName", Result = "Name.Space.TestSuiteName")]
        public String TestFullName(String nspace, String name)
        {
            var t = new TestSuite(name, nspace, Status.Success, SuiteType.TestFixture, TimeSpan.FromSeconds(1), 1, new List<ITestResult>());
            return t.FullName;
        }

        #endregion
    }
}
